package com.st;

import support.*;
import android.app.*;
import android.graphics.*;
import android.os.*;
import android.text.method.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.LinearLayout.*;

import java.io.*;
import java.util.*;

public class NetworkActivity extends Activity {
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// data
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

public EditText ttfUserid;
public EditText ttfPassword;

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// constructor
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
  super.onCreate(icicle);

  // create the panel to enclose everything
  View mainPanel = _createInputForm();

  // show the panel on the screen
  setContentView(mainPanel);
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// input form
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/** create the login form */
private ViewGroup _createInputForm() {
  LinearLayout panel = new LinearLayout(this);
  panel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  panel.setOrientation(LinearLayout.VERTICAL);

  // Userid : label and text field
  TextView lblUserid = new TextView(this);
  lblUserid.setText("Userid");
  lblUserid.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f);
  lblUserid.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  ttfUserid = new EditText(this);
  ttfUserid.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//  ttfUserid.setText("a");

  // Password : label and text field
  TextView lblPassword = new TextView(this);
  lblPassword.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f);
  lblPassword.setText("Password");
  lblPassword.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  ttfPassword = new EditText(this);
  ttfPassword.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  ttfPassword.setTransformationMethod(new PasswordTransformationMethod());
//  ttfPassword.setText("a");

  // login button
  final Button btnLogin = new Button(this);
  btnLogin.setText("Login");
  btnLogin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  btnLogin.setGravity(Gravity.CENTER);
  btnLogin.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      new GetDataFromWebService().execute(NetworkActivity.this);
      Log.i(getClass().getSimpleName(), "button was pressed");
    }
  });

  // actually adding the views (that have layout set on them) to the panel
  // userid
  panel.addView(lblUserid);
  panel.addView(ttfUserid);
  // password
  panel.addView(lblPassword);
  panel.addView(ttfPassword);
  // loginbutton
  panel.addView(btnLogin);

  return panel;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// display user profile data callback
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/** create a new panel to display the data and replace the existing content */
public void _displayUserProfile(Hashtable<DataKeys, Serializable> up) {

  String name = _getStringValue(up, DataKeys.str1, "user id");
  String pwd = _getStringValue(up, DataKeys.str2, "password");
  byte[] icon = _getByteRayValue(up, DataKeys.img1, null);

  View infoPanel = _createInfoPanel(name, pwd, icon);

  setContentView(infoPanel);

}
private View _createInfoPanel(String uid, String pwd, byte[] icon) {

  LinearLayout panel = new LinearLayout(this);
  panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  panel.setOrientation(LinearLayout.VERTICAL);

  // user id
  TextView lblName = new TextView(this);
  lblName.setText(uid);
  lblName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
  lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  // password
  TextView lblEmail = new TextView(this);
  lblEmail.setText(pwd);
  lblEmail.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
  lblEmail.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  // icon
  Bitmap bitmap = BitmapFactory.decodeStream(new ByteBuffer(icon).getInputStream());
  ImageView lblIcon = new ImageView(this);
  lblIcon.setImageBitmap(bitmap);
  lblIcon.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

  // quit button
  final Button btnRestart = new Button(this);
  btnRestart.setText("Restart");
  btnRestart.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  btnRestart.setGravity(Gravity.CENTER);
  btnRestart.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      setContentView(_createInputForm());
      Log.i(getClass().getSimpleName(), "restart button was pressed");
    }
  });

  // actually adding the views (that have layout set on them) to the panel
  // name
  panel.addView(lblName);
  panel.addView(lblEmail);
  panel.addView(lblIcon);
  panel.addView(btnRestart);

  ScrollView scrollPanel = new ScrollView(this);
  scrollPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  scrollPanel.addView(panel);

  return scrollPanel;

}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// helper methods to extract data from userprofile
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

private byte[] _getByteRayValue(Hashtable<DataKeys, Serializable> up, DataKeys key, byte[] defaultBytes) {
  try {
    return (byte[]) up.get(key);
  }
  catch (Exception e) {
    return defaultBytes;
  }
}

private String _getStringValue(Hashtable<DataKeys, Serializable> up, DataKeys key, String defaultValue) {
  try {
    return up.get(key).toString();
  }
  catch (Exception e) {
    return defaultValue;
  }
}


}// end NetworkActivity class